<?php

/**
 * This is the model class for table "Tracking".
 *
 * The followings are the available columns in table 'Tracking':
 * @property integer $TrackingIndex
 * @property string $CarrierNo
 * @property string $EBoxNumber
 * @property string $ToLAMethod
 * @property string $ShipDate
 * @property string $ArriveDate
 * @property string $ShippingStatus
 * @property string $ShippingType
 * @property string $Comment
 * @property string $CreateTime
 * @property string $UpdateTime
 * @property string $Login
 * @property string $Modifier
 * @property string $OETrackingStatus
 */
class Tracking extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Tracking the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'Tracking';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('CarrierNo, EBoxNumber, ShippingStatus, ShippingType, Login, Modifier', 'length', 'max' => 50),
            array('ToLAMethod, OETrackingStatus', 'length', 'max' => 100),
            array('Comment', 'length', 'max' => 1073741823),
            array('ShipDate, ArriveDate, CreateTime, UpdateTime', 'safe'),
            array('CarrierNo, EBoxNumber', 'unique'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('TrackingIndex, CarrierNo, EBoxNumber, ToLAMethod, ShipDate, ArriveDate, ShippingStatus, ShippingType, Comment, CreateTime, UpdateTime, Login, Modifier, OETrackingStatus', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'mids' => array(self::HAS_MANY, 'MidTab', 'TrackingIndex', 'order' => 'mids.CBoxNumber DESC'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'TrackingIndex' => 'Tracking Index',
            'CarrierNo' => '货柜编号<br />快递单号',
            'EBoxNumber' => '快递箱号',
            'ToLAMethod' => '运输方式',
            'ShipDate' => '运输日期',
            'ArriveDate' => '到达日期',
            'ShippingStatus' => '运输状态',
            'ShippingType' => '运输方式',
            'Comment' => '备注',
            'CreateTime' => '创建时间',
            'UpdateTime' => '更新时间',
            'Login' => '创建者',
            'Modifier' => '更新者',
            'OETrackingStatus' => 'OE状态跟踪',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('TrackingIndex', $this->TrackingIndex);
        $criteria->compare('CarrierNo', $this->CarrierNo, true);
        $criteria->compare('EBoxNumber', $this->EBoxNumber, true);
        $criteria->compare('ToLAMethod', $this->ToLAMethod, true);
        $criteria->compare('ShipDate', $this->ShipDate, true);
        $criteria->compare('ArriveDate', $this->ArriveDate, true);
        $criteria->compare('ShippingStatus', $this->ShippingStatus, true);
        $criteria->compare('ShippingType', $this->ShippingType, true);
        $criteria->compare('Comment', $this->Comment, true);
        $criteria->compare('CreateTime', $this->CreateTime, true);
        $criteria->compare('UpdateTime', $this->UpdateTime, true);
        $criteria->compare('Login', $this->Login, true);
        $criteria->compare('Modifier', $this->Modifier, true);
        $criteria->compare('OETrackingStatus', $this->OETrackingStatus, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->Login = $this->Modifier = Yii::app()->user->name;
                $this->CreateTime = $this->UpdateTime = date('Y-m-d H:i:s');
                $model->ShippingStatus = 'DataLogging';
            }
            $this->UpdateTime = date('Y-m-d H:i:s');
            $this->Modifier = Yii::app()->user->name;
            if(!$this->ShipDate){
                unset($this->ShipDate);
            }
            if(!$this->ArriveDate){
                unset($this->ArriveDate);
            }
        }
        return true;
    }
    
    protected function beforeDelete() {
        parent::beforeDelete();
        //删除前需要把属于他的mid删除
        $condition = new CDbCriteria();
        $condition->addCondition('TrackingIndex='.$this->TrackingIndex);
        $rowTable = MidTab::model()->findAll($condition);
        foreach($rowTable as $row){
            $row->delete();
        }
        unset($condition);
        
        if(MidTab::model()->find('TrackingIndex='.$this->TrackingIndex)){
            return false;
        }else{
            return true;
        }        
    }

}